package com.shanzhe.eshop.common.jdk;

import lombok.SneakyThrows;

import java.util.concurrent.CountDownLatch;

/**
 * @Author yinwei
 * @create 2021/12/12
 * @Version 1.0
 */
public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {
        final CountDownLatch downLatch = new CountDownLatch(2);

        new Thread(){
            @SneakyThrows
            @Override
            public void run() {
                Thread.sleep(1000);
                System.out.println("线程1准备执行countdown....");
                downLatch.countDown();
                System.out.println("线程1完成执行countdown....");
            }
        }.start();

        new Thread(){
            @SneakyThrows
            @Override
            public void run() {
                Thread.sleep(1000);
                System.out.println("线程2准备执行countdown....");
                downLatch.countDown();
                System.out.println("线程2完成执行countdown....");
            }
        }.start();

        System.out.println("阻塞等待....");
        downLatch.await();
        System.out.println("main线程执行....");

    }
}
